package com.project.common.result;

import lombok.Data;

@Data
public class Result<T>{
    private Integer code;//状态码
    private String message;//返回信息
    private T data;//数据

    //私有化，防止new
    private Result() {
    }

    public static <T> Result<T> build(T body, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        if (body != null) {
            result.setData(body);//封装数据
        }
        result.setCode(resultCodeEnum.getCode());//状态码
        result.setMessage(result.getMessage());//返回信息

        return result;
    }

    public static <T> Result<T> ok() {
        return build(null, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    public static <T> Result<T> fail() {
        return build(null, ResultCodeEnum.FAIL);
    }

    public static <T> Result<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL);
    }


    public Result<T> message(String msg){
        this.message=msg;
        return this;
    }

    public Result<T> code(Integer code){
        this.code=code;
        return this;
    }

}
